import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { user } from './entity/user.entity';
import { userDto } from './dto/user.dto';
import { Web3Service } from 'src/web3/web3.service';
@Injectable()
export class UserService {

    constructor(
        @InjectRepository(user)
        private readonly userRepository: Repository<user>,
        private web3:Web3Service
    ) { }
    //在数据库中查找用户
    async findOne(username: string) {
        return await this.userRepository.findOne({ where: { name: username } })
    }
    async findOneByAdd(userAdd: string) {
        return await this.userRepository.findOne({ where: { address: userAdd } })
    }
    //注册用户
    async addUser(userDto: userDto, newAdd: string) {
        const info: user = {
            id: undefined,
            name: userDto.name,
            password: userDto.password,
            address: newAdd,
            range: userDto.range
        }
        const newUser = this.userRepository.create(info);
        return await this.userRepository.save(newUser);
    }
    async getUserInfo(add:string){
        const info = await this.web3.getUser(add);
        return info;
    }
    async getAllUser(){
        const list =await this.web3.getAllUser();
        return list;
    }
    async setUserPass(user:user,newPassword:string){
        user.password=newPassword;
        await this.userRepository.save(user);
    }
    async setUserRange(user:user,newRange:number){
        user.range=newRange;
        await this.userRepository.save(user);
    }
}
